package com.ljx.thread.thread06;

import lombok.Data;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/11/1 19:41
 * @Version 1.0
 */
@Data
public class BankCard {
	private double balance;
	// true表示有钱
	private boolean flag=false;

	public void save(double money) throws InterruptedException {
		synchronized (this){
			if(flag==true){
				// 表示当前线程释放锁并进入等待队列
				wait();
			}
			balance=getBalance()+money;
			flag=true;
			System.out.println(Thread.currentThread().getName()+"存入"+money+"剩余"+balance);
			// 唤醒等待队列中的线程
			notify();


		}
	}

	public void take(double money){
		synchronized (this){
			if(flag==false) {
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			balance = getBalance() - money;
			flag=false;
			System.out.println(Thread.currentThread().getName()+"取出"+money+"剩余"+balance);
			notify();



		}
	}
}
